/*
 * @Description: 右键菜单弹窗集合
 * @Author: Rfan
 * @Date: 2022-06-29 11:51:56
 * @LastEditTime: 2022-07-05 17:14:16
 */

import {
  forwardRef,
  ForwardRefRenderFunction,
  useImperativeHandle,
  useMemo,
  useState
} from 'react';
import ArchivesMgtDialog from './dialogs/ArchivesMgt';
import CreateDataNodeDialog from './dialogs/CreateDataNode';
import CreateDescNodeDialog from './dialogs/CreateDescNode';
import CreateGroupNodeDialog from './dialogs/CreateGroupNode';
import { IFolderTreeNode } from '@/interfaces/folderTree';
import EditDescNodeDialog from './dialogs/EditDescNode';
import EditDataNodeDialog from './dialogs/EditDataNode';
import RenameNodeDialog from './dialogs/RenameNode';

export const DialogsName = {
  ARCHIVE_MGT: 'ARCHIVE_MGT',
  CREATE_DATA: 'CREATE_DATA',
  CREATE_DESC: 'CREATE_DESC',
  CREATE_GROUP: 'CREATE_GROUP',
  EDIT_DATA: 'EDIT_DATA',
  EDIT_DESC: 'EDIT_DESC',
  EDIT_GROUP: 'EDIT_GROUP',
  RENAME: 'RENAME'
};

interface ContextDialogsProps {}

const ContextDialogs: ForwardRefRenderFunction<any, ContextDialogsProps> = (props, ref) => {
  // 选中的节点
  const [node, setNode] = useState(null);
  // 档案节点管理弹窗状态
  const [archiveMgtVisible, setArchiveMgtVisible] = useState(false);
  // 添加数据节点弹窗状态
  const [createDataVisible, setCreateDataVisible] = useState(false);
  // 添加描述节点弹窗状态
  const [createDescVisible, setCreateDescVisible] = useState(false);
  // 添加分组节点弹窗状态
  const [createGroupVisible, setCreateGroupVisible] = useState(false);
  // 编辑数据节点弹窗状态
  const [editDataVisible, setEditDataVisible] = useState(false);
  // 编辑描述节点弹窗状态
  const [editDescVisible, setEditDescVisible] = useState(false);
  // 编辑分组节点弹窗状态
  const [editGroupVisible, setEditGroupVisible] = useState(false);
  // 重命名节点弹窗状态
  const [renameVisible, setRenameVisible] = useState(false);

  // 暴露给父组件的方法
  useImperativeHandle(ref, () => ({
    showDialogFunc
  }));

  const showDialogFunc = (name: string, type: number, treeNode: IFolderTreeNode): void => {
    setNode(treeNode);
    switch (name) {
      // 节点管理
      case DialogsName.ARCHIVE_MGT:
        setArchiveMgtVisible(true);
        break;
      // 创建数据节点
      case DialogsName.CREATE_DATA:
        setCreateDataVisible(true);
        break;

      // 创建描述节点
      case DialogsName.CREATE_DESC:
        setCreateDescVisible(true);
        break;

      // 创建描述节点
      case DialogsName.CREATE_GROUP:
        setCreateGroupVisible(true);
        break;

      // 编辑数据节点
      case DialogsName.EDIT_DATA:
        setEditDataVisible(true);
        break;

      // 编辑描述节点
      case DialogsName.EDIT_DESC:
        setEditDescVisible(true);
        break;

      // 编辑描述节点
      case DialogsName.EDIT_GROUP:
        setEditGroupVisible(true);
        break;

      // 重命名
      case DialogsName.RENAME:
        setRenameVisible(true);
        break;

      default:
        break;
    }
  };

  return (
    <>
      {/* 档案节点管理弹窗 */}
      {useMemo(
        () => (
          <ArchivesMgtDialog
            visible={archiveMgtVisible}
            onClose={() => {
              setArchiveMgtVisible(false);
            }}
          />
        ),
        [archiveMgtVisible]
      )}

      {/* 添加数据节点弹窗 */}
      {useMemo(
        () => (
          <CreateDataNodeDialog
            visible={createDataVisible}
            node={node}
            onClose={() => {
              setCreateDataVisible(false);
            }}
          />
        ),
        [createDataVisible, node]
      )}

      {/* 编辑数据节点弹窗 */}
      {useMemo(
        () => (
          <EditDataNodeDialog
            visible={editDataVisible}
            node={node}
            onClose={() => {
              setEditDataVisible(false);
            }}
          />
        ),
        [editDataVisible, node]
      )}

      {/* 添加描述节点弹窗 */}
      {useMemo(
        () => (
          <CreateDescNodeDialog
            visible={createDescVisible}
            node={node}
            onClose={() => {
              setCreateDescVisible(false);
            }}
          />
        ),
        [createDescVisible, node]
      )}

      {/* 编辑描述节点弹窗 */}
      {useMemo(
        () => (
          <EditDescNodeDialog
            visible={editDescVisible}
            node={node}
            onClose={() => {
              setEditDescVisible(false);
            }}
          />
        ),
        [editDescVisible, node]
      )}

      {/* 添加分组节点弹窗 */}
      {useMemo(
        () => (
          <CreateGroupNodeDialog
            visible={createGroupVisible}
            node={node}
            onClose={() => {
              setCreateGroupVisible(false);
            }}
          />
        ),
        [createGroupVisible, node]
      )}

      {/* 编辑分组节点弹窗 */}
      {useMemo(
        () => (
          <RenameNodeDialog
            visible={editGroupVisible}
            title="编辑节点名称"
            node={node}
            onClose={() => {
              setEditGroupVisible(false);
            }}
          />
        ),
        [editGroupVisible, node]
      )}

      {/* 编辑分组节点弹窗 */}
      {useMemo(
        () => (
          <RenameNodeDialog
            visible={renameVisible}
            node={node}
            onClose={() => {
              setRenameVisible(false);
            }}
          />
        ),
        [renameVisible, node]
      )}
    </>
  );
};

export default forwardRef(ContextDialogs);
